package cn.elead.chaos.framework.web.serializer;

import java.io.IOException;
import java.time.LocalDate;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import cn.elead.chaos.core.util.DateUtil;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 接受字符串类型的时间转成LocalDate
 * 
 * @author luopeng
 *
 */
public class LocalDateJsonDeserializer extends JsonDeserializer<LocalDate> {

	@Override
	public LocalDate deserialize(JsonParser jsonParser, DeserializationContext ctxt)
			throws IOException, JsonProcessingException {
		try {
			if (ObjectUtil.isNotEmpty(jsonParser) && StrUtil.isNotEmpty(jsonParser.getText())) {
				return DateUtil.stringToLocalDate(jsonParser.getText());
			} else {
				return null;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
